package com.gin.supermarket_inventory_system.inventory.controller;

import com.gin.common.constant.ApiPath;
import com.gin.database.validation.EntityId;
import com.gin.security.Constant.Security;
import com.gin.spring.annotation.MyRestController;
import com.gin.spring.exception.BusinessException;
import com.gin.spring.vo.response.Res;
import com.gin.supermarket_inventory_system.inventory.docket.InventoryDocket;
import com.gin.supermarket_inventory_system.inventory.dto.form.PurveyorAddForm;
import com.gin.supermarket_inventory_system.inventory.dto.form.PurveyorUpdateForm;
import com.gin.supermarket_inventory_system.inventory.dto.vo.PurveyorVo;
import com.gin.supermarket_inventory_system.inventory.service.InventoryReceiptService;
import com.gin.supermarket_inventory_system.inventory.service.PurveyorService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 *
 * @since : 2023/9/26 16:37
 * @author : ginstone
 * @version : v1.0.0
 **/
@Slf4j
@MyRestController(PurveyorController.API_PREFIX)
@Tag(name = PurveyorController.GROUP_NAME)
@RequiredArgsConstructor
public class PurveyorController {
    /**
     *  接口路径前缀
     */
    public static final String API_PREFIX = InventoryDocket.API_PREFIX + "/purveyor";
    /**
     *  接口文档分组名称
     */
    public static final String GROUP_NAME = "供应商管理";
    /**
     * 本接口使用的主Service类
     */
    private final PurveyorService service;
    private final InventoryReceiptService receiptService;


    @PostMapping(ApiPath.ADD)
    @Operation(summary = "添加")
    @PreAuthorize(Security.PRE_AUTHORITY_URI_OR_ADMIN)
    public Res<Void> postAdd(@RequestBody @Validated PurveyorAddForm form,
                             @SuppressWarnings("unused") HttpServletRequest request) {
        service.save(form.toPo());
        return Res.success();
    }

    @PostMapping(ApiPath.UPDATE)
    @Operation(summary = "修改")
    @PreAuthorize(Security.PRE_AUTHORITY_URI_OR_ADMIN)
    public Res<Void> postUpdate(@RequestBody @Validated PurveyorUpdateForm form,
                                @SuppressWarnings("unused") HttpServletRequest request) {
        service.updateById(form.toPo());
        return Res.success();
    }

    @PostMapping(ApiPath.DEL)
    @Operation(summary = "删除")
    @PreAuthorize(Security.PRE_AUTHORITY_URI_OR_ADMIN)
    public Res<Void> postDel(
            @RequestParam @EntityId(service = PurveyorService.class) long id,
            @SuppressWarnings("unused") HttpServletRequest request
    ) {
        // 已被单据使用的供应商禁止删除
        final long count = receiptService.countByPurveyorId(id);
        if (count > 0) {
            throw BusinessException.of(HttpStatus.BAD_REQUEST, String.format("该供应商被 %d 条库存单据使用, 禁止删除", count));
        }

        service.removeById(id);
        return Res.success("删除成功");
    }

    @GetMapping(ApiPath.LIST_ALL)
    @Operation(summary = "查询所有")
    public Res<List<PurveyorVo>> getListAll() {
        final List<PurveyorVo> data = PurveyorVo.toVoList(service.list());
        return Res.of(data);
    }
}